package com.detrum.usbdevice.fragment;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.detrum.usbdevice.R;
import com.detrum.utils.MyMethod;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link SelectFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link SelectFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class SelectFragment extends Fragment implements CompoundButton.OnCheckedChangeListener{

    private OnFragmentInteractionListener mListener;

    private Context context;
    private CheckBox channel_cb,battery_cb,jieping_cb,video_cb;

    public static SelectFragment newInstance() {
        SelectFragment fragment = new SelectFragment();
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_select2, container, false);
        initView(view);
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        boolean[] values= MyMethod.getSelectItem(context,new String[]{"battery","channel","video","capture"});
        battery_cb.setChecked(values[0]);
        channel_cb.setChecked(values[1]);
        video_cb.setChecked(values[2]);
        jieping_cb.setChecked(values[3]);
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    private void initView(View view) {
        channel_cb = view.findViewById(R.id.channel_cb);
        battery_cb = view.findViewById(R.id.battery_cb);
        jieping_cb = view.findViewById(R.id.jieping_cb);
        video_cb = view.findViewById(R.id.video_cb);
        battery_cb.setOnCheckedChangeListener(this);
        jieping_cb.setOnCheckedChangeListener(this);
        video_cb.setOnCheckedChangeListener(this);
        channel_cb.setOnCheckedChangeListener(this);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (mListener != null) {
            mListener.onFragmentClick(buttonView,isChecked);
        }
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        void onFragmentInteraction(Uri uri);
        void onFragmentClick(CompoundButton buttonView, boolean isChecked);
    }
}
